package com.core.db.condition;

import java.util.ArrayList;
import java.util.List;

/**
 * 默认的查询条件封装类
 * 
 * @author 殷田
 *
 */
class DefaultWhere implements Where
{
	private StringBuffer buffer = new StringBuffer();
	private List< Object > values = new ArrayList< Object >();

	@Override
	public String getSql()
	{
		if ( buffer == null )
		{
			return "";
		}
		return buffer.toString();
	}

	@Override
	public Where reset()
	{
		buffer = new StringBuffer();
		values.clear();
		return this;
	}

	@Override
	public Where and( String column, String operator, Object value )
	{
		addWhere( buffer.length() == 0 ? "WHERE " : " AND ", column, operator, value );
		return this;
	}

	@Override
	public Where or( String column, String operator, Object value )
	{
		addWhere( buffer.length() == 0 ? "WHERE " : " OR ", column, operator, value );
		return this;
	}

	private synchronized void addWhere( String connector, String column, String operator,
					Object value )
	{
		buffer.append( connector ).append( column ).append( operator ).append( "?" );
		values.add( value );
	}

	@Override
	public List< Object > values()
	{
		return values;
	}
}
